from playwright.sync_api import sync_playwright, expect, Page
from interface.page_login_save_cookie import page_login_save_cookies, 跳转
import time
import pytest
from utils.tools import 日期控件, get_path

计划名称 = " "
time_out = 1000


def test_文件上传和下载(page):
    page_login_save_cookies(page, "m18515393438@163.com", "chenshi0802!")
    page.wait_for_timeout(time_out)
    跳转(page, "/ship/products")
    page.wait_for_timeout(time_out)
    page.get_by_text("测试专用").click()
    page.get_by_text("工单").click()
    page.wait_for_timeout(time_out)
    while True:
        try:
            page.locator('//a[@thyactionicon="list"]').click(timeout=1000)
            page.get_by_text("导入").click()
            break
        except:
            pass
    with page.expect_download() as f:
        page.locator("a").get_by_text("下载模板").click()
    file_path = get_path(f"download/{time.time_ns()}.xlsx")
    f.value.save_as(file_path)
    page.set_input_files('//input[@type="file"]', get_path("upload/testupload.xlsx"))  # 上传定位到input元素
    expect(page.get_by_text("testupload.xlsx")).to_be_visible()
    page.get_by_text("取消").click()

    page.locator('//span[text()="客户"]').click()
    page.get_by_text("新建").click()
    # page.locator('//button[@thyicon="plus"]').click()

    with page.expect_file_chooser() as f:
        page.get_by_text("点击上传").click()
    f.value.set_files([get_path("upload/1400.jpg")])  # 目前只支持一张图片
    page.get_by_text("确定").last.click()  # last 从这个集合中取出最后一个元素  .first 取集合中的第一个元素
    page.get_by_text("取消").click()